var app = getApp();


Page({

  /**
   * 页面的初始数据
   */
  data: {
    topImg: '../../img/logo.png',
    storeName: '一起服装',
    refereeCode: '',
    JSESSIONID: '',
    session3rd: ''
  },

  // 确认授权
  userLogin: function(e) {
    var that = this
    const loadRequest = () => {
      return new Promise((resolve, reject) => {
        wx.getSetting({
          success: res => {
            if (res.authSetting['scope.userInfo']) {
              wx.login({
                //获取code
                success: function(res) {
                  wx.request({
                    url: app.globalData.api + 'wechat/miniProgramLoginStatus',
                    method: 'POST',
                    data: {
                      jsCode: res.code
                    },
                    header: {
                      // 'content-type': 'application/json'
                    },
                    success: function(res) {
                      // console.log("第一步获取JSESSIONID/session3rd", res)
                      that.setData({
                        session3rd: res.data.data.session3rd
                      })
                      wx.getUserInfo({
                        withCredentials: true,
                        success: function(res) {
                          resolve(res); //必须有，否则该request方法无法执行
                          that.setData({
                            userInfo: res.userInfo,
                            encryptedData: res.encryptedData,
                            iv: res.iv,
                            rawData: res.rawData,
                            signature: res.signature
                          })
                          //保存用户的个人信息
                          wx.setStorageSync('userInfo', that.data.userInfo)
                          console.log("第二步获取encryptedData/iv", res)
                        }
                      })
                    }
                  })
                }
              })
            } else {
              this.setData({
                hasUserInfo: false,
              })
              console.log('您取消了授权')
            }
          }
        })
      })
    }


    //登陆
    loadRequest().then((res) => {
      // console.log("参数", "iv", this.data.iv, "JSESSIONID", this.data.JSESSIONID, "encryptedData", this.data.encryptedData, "3rd", this.data.session3rd)
      wx.request({
        url: app.globalData.api + 'user/wechatMiniProgram/login',
        method: 'POST',
        data: {
          encryptedData: this.data.encryptedData,
          session3rd: this.data.session3rd,
          iv: this.data.iv,
          rawData: this.data.rawData,
          signature: this.data.signature,

        },
        header: {
          "Content-Type": 'application/json'
        },
        success: function(res) {
          console.log(res)
          if (res.data.code == 200) {
            wx.setStorageSync('token', res.data.accessToken)
            wx.redirectTo({
              url: '/pages/sign/sign',
            })
          }else{
            wx.showModal({
              title: '提示',
              content: res.data.msg + res.data.errorMsg,
            })
          }
        }
      })
    })
  },




  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

    // 从缓存读取信息，判断用户是否已经授权
    var token = wx.getStorageSync("token")

    wx.login({
      success: res => { //第一步，拿code
        console.log(111, res)
        app.globalData.code = res.code
      }
    })
    if (token) {
      wx.redirectTo({
        url: '/pages/sign/sign',
      })
    }

  }

})